<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addPerm(1, '0')">添加权限</el-button>
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table :data="permissionList" border row-key="id">
        <el-table-column label="名称" prop="name" />
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{ row }">
            <el-button v-if="row.type === 1" type="text" @click="addPerm(2, row.id)">添加</el-button>
            <el-button type="text" @click="editPerm(row.id)">编辑</el-button>
            <el-button type="text" @click="delPerm(row.id)">删除</el-button>
          </template>
        </el-table-column>

      </el-table>
    </div>
    <el-dialog :title="dialogTitle" :visible.sync="dialogShow">
      <el-form ref="form" :model="permDetail" :rules="rules" label-width="120px" @close="btnCancel">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="permDetail.name" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="permDetail.code" style="width: 90%" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="permDetail.description" style="width: 90%" />
        </el-form-item>
        <el-form-item label="企业可见">
          <el-switch
            v-model="permDetail.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, getPermDetailById, updatePermisssion, delPermission, addPermission } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
export default {
  name: 'PermissionIndex',
  data() {
    return {
      permissionList: [], // 权限点数据
      // 权限点详情 表单数据
      permDetail: {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: null,
        pid: ''
      },
      dialogShow: false, // 弹层显示
      // 表单校验
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.permDetail.id ? '编辑权限' : '新增权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      this.permissionList = tranListToTreeData(await getPermissionList(), '0')
    },
    async editPerm(id) {
      this.dialogShow = true
      this.permDetail = await getPermDetailById(id)
    },
    btnCancel() {
      this.dialogShow = false
      // 重置表单
      this.permDetail = {
        enVisible: '',
        name: '',
        code: '',
        description: '',
        type: null,
        pid: ''
      }
      this.$refs.form.resetFields()
    },
    async btnOK() {
      try {
        await this.$refs.form.validate()
        if (this.permDetail.id) {
          await updatePermisssion(this.permDetail)
          this.$message.success('更新成功')
        } else {
          await addPermission(this.permDetail)
          this.$message.success('添加成功')
        }
        this.getPermissionList()
        this.btnCancel()
      } catch (err) {
        console.log(err)
      }
    },
    async delPerm(id) {
      try {
        await this.$confirm('是否删除此权限点', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delPermission(id)
        this.$message.success('删除成功')
        this.getPermissionList()
      } catch (err) {
        console.log(err)
      }
    },
    addPerm(type, pid) {
      this.dialogShow = true
      this.permDetail.type = type
      this.permDetail.pid = pid
    }
  }
}
</script>

<style>

</style>
